package by.grsu.hdm.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import by.grsu.hdm.model.Media;
import by.grsu.hdm.model.MediaPlayer;

/**
 * Servlet implementation class MediaPlayerServlet
 */
@WebServlet("/MediaPlayerServlet")
public class MediaPlayerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MediaPlayerServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		MediaPlayer media = new MediaPlayer();
		media.addMediaFile(new Media("Opeth-Bleak",".mp3"));
		media.addMediaFile(new Media("Opeth-Twillight is my robe",".mp3"));
		media.addMediaFile(new Media("Queen-Love of my life",".mp3"));
		media.addMediaFile(new Media("Queen-Somebody to love",".mp3"));
		
		response.setContentType("text/html");
		
		PrintWriter out = response.getWriter();

        out.println("<html>");
        out.println("<head>");
        out.println("<title>MediaPlayer</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h30>PlayList</h30><br>");
        out.println("<br/>");
        if (request.getParameter("id")!=null)
        {
                try {
                        int id = Integer.parseInt( request.getParameter("id"));
                                out.println(media.play(id-1)+"<br>");
                                out.println("<br/>");
                                out.println("<form action=MediaPlayerServlet");
                                out.println("<br><br/>");
                                out.println("<input value='return' type= 'submit'>");
                                out.println("</form>");
                               
                        } catch (Exception e) {
                                out.println(e.getMessage()+"<br>");
                        }
        }
        else
        {
                try {
                        for (int i = 0; i < media.getCountFiles(); i++) {
                                out.println("ID:"+(i+1)+" "+media.getName((i))+"<br>");
                                }
                        } catch (Exception e) {
                                out.println(e.getMessage()+"<br>");
                        }

                out.println("<form action=MediaPlayerServlet");
                out.println("<br><br/>");
                out.println("ID:");
                out.println("<input name = id>");
                out.println("<input value='play' type= 'submit'>");
                out.println("</form>");
        }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
